package cn.com.yusys.yusp.admin.web.rest;

import cn.com.yusys.yusp.admin.domain.WcrmAgriculturalSubjectVisitInfo;
import cn.com.yusys.yusp.admin.domain.WcrmCustomerVisitInfo;
import cn.com.yusys.yusp.commons.excel.ExcelTaskManager;
import cn.com.yusys.yusp.commons.mapper.QueryModel;
import cn.com.yusys.yusp.commons.web.rest.dto.ResultDto;
import cn.com.yusys.yusp.mobile.repository.mapper.CustomerMainBodyVisitMapper;
import cn.com.yusys.yusp.mobile.repository.mapper.CustomerVisitMapper;
import cn.com.yusys.yusp.mobile.service.CustomerVisitService;
import com.github.pagehelper.PageHelper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/customerVisitResource")
public class CustomerVisitExcelResource {

    @Resource
    CustomerVisitMapper customerVisitMapper;

    @Resource
    private ExcelTaskManager excelTaskManager;
    private String pojoClassName = WcrmCustomerVisitInfo.class.getName();
    private String exportSql = "queryAllByLimitVisit";

    //查询客户走访列表
    @GetMapping("/queryAllByLimitVisit")
    public ResultDto<Object> getCustomerVisitList(QueryModel queryModel) {
        PageHelper.startPage(queryModel.getPage(), queryModel.getSize());
        String roleId= (String) queryModel.getCondition().get("roleId");
        String orgId = (String)queryModel.getCondition().get("orgId");
        List<Map<String, Object>> list = null;
        //分行行长查询显示其所有属于其所属的机构下客户
        if (orgId.equals("070667XN0") || orgId.equals("070667XN1") ||orgId.equals("070667860") ||orgId.equals("070667XN3")){
            queryModel.getCondition().put("roleLevel", "1");
            list = customerVisitMapper.queryAllByLimitVisit(queryModel);
        }
        //查询支行下所属的所有客户经理的用户 Sub-branch Manager
        if (roleId.equals("R0001") || roleId.equals("R0002") || roleId.equals("R0009")){
            queryModel.getCondition().put("roleLevel", "3");
            list = customerVisitMapper.queryAllByLimitVisit(queryModel);
        }
        //查询所有用户
        if (roleId.equals("R0008") || roleId.equals("R0007") || roleId.equals("R001")){
            queryModel.getCondition().put("roleLevel", "0");
            list = customerVisitMapper.queryAllByLimitVisit(queryModel);
        }
        //支行行长查询，显示其机构下所有客户
        if (roleId.equals("R0006") || roleId.equals("R0005") || roleId.equals("R0003") || roleId.equals("R0004")){
            queryModel.getCondition().put("roleLevel", "2");
            list = customerVisitMapper.queryAllByLimitVisit(queryModel);
        }

        PageHelper.clearPage();
        return new ResultDto<>(list);
    }

    @GetMapping("/exportExcel")
    public ResultDto<String> exportExcel(QueryModel queryModel){
        CustCenterResource.judgeUserJurisdiction(queryModel, true);
        String taskId = excelTaskManager.addExcelExportTask(exportSql, null, queryModel, pojoClassName);
        return new ResultDto<>(taskId);
    }

}
